package com.yyt.threadpooldemo.demo;

/**
 * 线程的状态
 * NEW,
 * RUNNABLE,
 * BLOCKED,
 * WAITING,
 * TIMED_WAITING,
 * TERMINATED;
 *
 * @author: yeyangtao
 * @date: 2020/4/13 19:34
 */
public class ThreadDemo {
    public static void main(String[] args) {
        Account account = new Account(100, "yyt");
        ThreadClass tc1 = new ThreadClass(account, 80, "1");
        ThreadClass tc2 = new ThreadClass(account, 90, "2");
        tc1.start();
        tc2.start();
    }
}

//账户
class Account {
    int money;
    String name;

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }
}

class ThreadClass extends Thread {
    Account a; //取钱的账户
    int getMoney; //单个人取的钱数
    int getTotal; //多个人取的总数

    public ThreadClass(Account a, int getMoney, String name) {
        super(name);//Thread可以设置name
        this.a = a;
        this.getMoney = getMoney;
    }
    @Override
    public void run() {
        if (a.money - getMoney < 0) //添加也没用
        {
            return;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        a.money -= getMoney;
        getTotal += getMoney;
        System.out.println(this.getName() + "-->账户余额为:" + a.money);
        System.out.println(this.getName() + "-->取钱总数为:" + getTotal);
    }
}

